local skel = fk.CreateSkill {
  name = "lb__tianhui",
  tags = { Skill.Wake },
}

Fk:loadTranslationTable {
  ["lb__tianhui"] = "天恚",
  [":lb__tianhui"] = "<b>觉醒技</b>，当你进入濒死状态时，你回满体力值并失去" ..
      "<a href=':lb__qingshi'><font color='red'>〖擎世〗</font></a>，然后选择一名成为过你" ..
      "<a href=':lb__chouzhuo'><font color='red'>〖仇灼〗</font></a>目标的角色重复以下流程至一方死亡或「<b>仇灼</b>」牌耗尽：" ..
      "若首张「<b>仇灼</b>」牌为黑色/红色，你将之当火【杀】对其/你使用。",

  ["#lb__tianhui"] = "天恚：选择一名成为过你〖仇灼〗目标的角色将「<b>仇灼</b>」牌当火【杀】对你或其使用",

  ["$lb__tianhui1"] = "以旧日余烬，为来世破晓！",
  ["$lb__tianhui2"] = "以旧日余烬，为来世破晓！",
}

skel:addEffect(fk.EnterDying, {
  can_trigger = function(self, event, target, player, data)
    return player:usedSkillTimes(skel.name, Player.HistoryGame) == 0 and target == player and player:hasSkill(skel.name)
  end,
  can_wake = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:recover {
      who = player,
      num = player.maxHp - player.hp,
      skillName = skel.name,
      recoverBy = player,
    }
    room:handleAddLoseSkills(player, "-lb__qingshi")
    local tos = table.filter(player:getTableMark("@[player]lb__chouzhuo"), function(id)
      return room:getPlayerById(id):isAlive()
    end)
    if player:isAlive() and #tos > 0 and #player:getPile("$lb__chouzhuo") > 0 then
      tos = table.map(tos, function(id)
        return room:getPlayerById(id)
      end)
      if #tos > 1 then
        tos = room:askToChoosePlayers(player, {
          targets = tos,
          min_num = 1,
          max_num = 1,
          skill_name = skel.name,
          cancelable = false,
          prompt = "#lb__tianhui",
        })
      end
      local to = tos[1]
      while player:isAlive() and to:isAlive() and #player:getPile("$lb__chouzhuo") > 0 do
        local card = Fk:getCardById(player:getPile("$lb__chouzhuo")[1])
        local use
        if card.color == Card.Black then
          use = room:useVirtualCard("fire__slash", {card.id}, player, to, skel.name, true)
        elseif card.color == Card.Red then
          local fire_slash = Fk:cloneCard("fire__slash")
          fire_slash:addSubcard(card)
          fire_slash.skillName = skel.name
          if not player:prohibitUse(fire_slash) then
            use = { ---@type UseCardDataSpec
              from = player,
              tos = {player},
              card = fire_slash,
              extraUse = true,
            }
            room:useCard(use)
          end
        end
        if not use and player:isAlive() then
          room:moveCardTo(card, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, skel.name, "$lb__chouzhuo", true, player)
        end
      end
    end
  end,
})

return skel